﻿Imports System.Data.SqlClient

Public Class FormProduct
    Private Sub FormProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.dgv)
        LoadData()
    End Sub

    Private Sub LoadData()
        Cmd.Parameters("@S") = "%" & txtSearch.Text & "%"
        Me.dgv.DataSource = Cmd.ExecuteDataTable("SELECT * FROM Product WHERE ProductName LIKE @S AND Active=1;")
    End Sub


    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Dim row = dgv.SelectedRows(0)
        Dim id = row.Cells(0).Value

        Cmd.Parameters("@Id") = id
        Cmd.ExecuteNonQuery("UPDATE Product SET Active=0 WHERE ProductId=@Id")

        LoadData()

    End Sub



    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgv.SelectedRows(0)

        Dim frm As New FormProductEditor()
        frm.txtProductId.Text = row.Cells("ProductId").Value
        frm.txtProductCode.Text = row.Cells("ProductCode").Value
        frm.txtProductName.Text = row.Cells("ProductName").Value
        frm.cboCategory.Text = row.Cells("CategoryId").Value
        frm.txtPrice.Text = row.Cells("Price").Value
        frm.txtMinimumPrice.Text = row.Cells("MinimumPrice").Value
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            LoadData()
        End If

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        FormProductEditor.ShowDialog()
    End Sub
End Class